package cn.wolfcode.rbac.web.controller;


import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
	private IDepartmentService service;
	@RequestMapping("/list")
	@RequiresPermissions(value = {"部门列表","department:list"},logical = Logical.OR)
	public String list(Model model, @ModelAttribute("qo")QueryObject qo){
		PageInfo result = service.query(qo);
		model.addAttribute("result", result);
		return "department/list";
	}
	@RequestMapping("/input")
	@RequiresPermissions(value = {"部门编辑","department:input"},logical = Logical.OR)
	public String input(Model model,Long id){
		if(id!=null){
			Department entity = service.selectOne(id);
			model.addAttribute("entity", entity);
		}
		return "department/input";
	}
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	@RequiresPermissions(value = {"部门更新","department:saveOrUpdate"},logical = Logical.OR)
	public JsonResult saveOrUpdate(Department u){
		try {
			service.saveOrUpdate(u);
			return new JsonResult("成功",true);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult(e.getMessage(),false);
		}
	}
	@RequestMapping("/delete")
	@ResponseBody
	@RequiresPermissions(value = {"部门删除","department:delete"},logical = Logical.OR)
	public JsonResult list(Long id){
		try {
			service.delete(id);
			return new JsonResult("成功",true);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult(e.getMessage(),false);
		}

	}
}
